const app = getApp();
const pageSize = 15;
Page({
  data: {
    list: [],
    empty: false,
    loadMore: false,
    noMore: false,
    modal: false,
    modalContent: "",
  },

  onLoad: function () {
    this.page = 1;
    this.setData({ //添加区域码后会调用本页面的onLoad，重置list使页面回到顶部
      list: []
    })
    this.getRegionCodeList();
  },

  navAddRegion() {
    wx.navigateTo({
      url: '../add/add',
    })
  },

  handleClick(e) {
    const index = e.detail.index;
    if (index == 0) {
      this.hide();
    } else {
      wx.showLoading({
        mask: true
      });
      app.ajax({
        url: "Partner/CardMother/frCardMother",
        data: {
          code_id: this.selectId
        },
        success: res => {
          wx.hideLoading();
          if (res.data.code == 1000) {
            app.showToast(res.data.message, "success");
            const list = this.data.list;
            list[this.selectIndex].is_frozen = list[this.selectIndex].is_frozen == 1 ? -1 : 1;
            this.setData({
              list
            })
            this.hide();
          } else {
            app.showToast(res.data.message);
          }
        },
        error: e => {
          wx.hideLoading()
        }
      })
    }
  },

  hide() {
    this.setData({
      modal: false
    })
    delete(this.selectIndex);
    delete(this.selectId);
  },


  showModal(e) {
    const {
      id,
      index,
      frozen,
      name
    } = e.currentTarget.dataset;
    let modalContent = frozen == 1 ? `是否冻结[${name}]区域码？` : `是否解冻[${name}]区域码？`;
    this.selectIndex = index;
    this.selectId = id;
    this.setData({
      modal: true,
      modalContent
    })
  },

  onReachBottom: function () {
    if (this.data.noMore) {
      return false;
    } else {
      ++this.page;
      this.getRegionCodeList();
    }
  },

  //获取区域码列表
  getRegionCodeList() {
    app.ajax({
      url: "Partner/CardMother/cardMotherList",
      data: {
        page: this.page,
        pageSize,
      },
      success: res => {
        if (res.data.code == 1000) {
          let source = res.data.data.data,
            list;
          let {
            page_count
          } = res.data.data.page;
          if (this.page == 1) {
            list = source;
          } else {
            list = this.data.list.concat(source);
          }
          this.setData({
            list,
            empty: page_count == 0 ? true : false,
            loadMore: page_count > this.page ? true : false,
            noMore: page_count <= this.page ? true : false
          })
        } else {
          let empty = false;
          if (this.page == 1) {
            empty = true;
          }
          this.setData({
            empty,
            loadMore: false,
            noMore: true
          })
        }
      },
      error: e => {

      }
    })
  }

})